//+---------------------------------------------------------------------------
//
// Copyright (C) Microsoft Corporation, 2008
//
// File: EventLogger.cs
//
// Contents: Event Logger singleton
//
// Created by: yunlin April 15, 2008
//
//----------------------------------------------------------------------------

using System;
using System.Diagnostics;
using System.ComponentModel;
using System.Threading;
using System.Reflection;

namespace Microsoft.Reputation.Services
{
    public static class EventLogger
    {
        private static string EventLoggerSource = string.Format("Delivery Service V{0}",
            Assembly.GetExecutingAssembly().GetName().Version.Major.ToString());
        private const int ServiceStartingEventId = 100;
        private const int ServiceStartedEventId = 101;
        private const int ServiceStoppingEventId = 102;
        private const int ServiceStoppedEventId = 103;

        private const int MrsApiProcessCacheEventId = 210;
        private const int MrsApiCallbackCacheEventId = 211;

        private const int MrsApiProcessDatabaseEventId = 212;
        private const int MrsApiCallbackDatabaseEventId = 213;

        private const int MrsApiProcessResponseEventId = 214;
        private const int MrsApiCallbackResponseEventId = 215;

        private const int MrsApiGetDataBlockEventId = 216;
        private const int MrsApiCategorizeResponseEventId = 217;

        private const int GetOverridesThreadEventId = 218;
        private const int TopLevelExceptionEventId = 219;

        private static int s_antiEventIdBase = 100;

        private static int s_MrsApiProcessCacheState = CommonEventLogger.EventSucceedState;
        private static int s_MrsApiCallbackCacheState = CommonEventLogger.EventSucceedState;
        private static int s_MrsApiProcessDatabaseState = CommonEventLogger.EventSucceedState;
        private static int s_MrsApiCallbackDatabaseState = CommonEventLogger.EventSucceedState;
        private static int s_MrsApiProcessResponseState = CommonEventLogger.EventSucceedState;
        private static int s_MrsApiCallbackResponseState = CommonEventLogger.EventSucceedState;
        private static int s_MrsApiGetDataBlockState = CommonEventLogger.EventSucceedState;
        private static int s_MrsApiCategorizeResponseState = CommonEventLogger.EventSucceedState;
        private static int s_GetOverridesThreadState = CommonEventLogger.EventSucceedState;
        private static int s_TopLevelExceptionThreadState = CommonEventLogger.EventSucceedState;

        public static EventLog EventLog { get; internal set; }

        public static void Initialize()
        {
            if (DeliveryServiceConfig.EventLoggerDisabled)
            {
                return;
            }

            if (!EventLog.SourceExists(EventLoggerSource))
            {
                BaseException baseEx = BaseException.Create(
                    "Event Logger are enabled, but the Event Logger Source for Delivery Service does not exist."
                       + "Create the Event Logger Source or disable it in web.config",
                    "Initializing Event Logger for DeliveryService",
                    "EventLogger");
                throw baseEx;
            }

            CommonEventLogger.Initialize(EventLoggerSource);
            EventLog = CommonEventLogger.EventLog;
        }

        public static void Shutdown()
        {
            CommonEventLogger.Shutdown();
        }

        public static void LogMrsProcessCacheApiSuccess()
        {
            CommonEventLogger.LogSuccessEvent(
                ref s_MrsApiProcessCacheState,
                MrsApiProcessCacheEventId + s_antiEventIdBase);
        }

        public static void LogMrsProcessCacheApiFailure(string errorMsg)
        {
            CommonEventLogger.LogFailureEvent(
                ref s_MrsApiProcessCacheState,
                MrsApiProcessCacheEventId,
                errorMsg);
        }

        public static void LogMrsProcessDatabaseApiSuccess()
        {
            CommonEventLogger.LogSuccessEvent(
                ref s_MrsApiProcessDatabaseState,
                MrsApiProcessDatabaseEventId + s_antiEventIdBase);
        }

        public static void LogMrsProcessDatabaseApiFailure(string errorMsg)
        {
            CommonEventLogger.LogFailureEvent(
                ref s_MrsApiProcessDatabaseState,
                MrsApiProcessDatabaseEventId,
                errorMsg);
        }

        public static void LogMrsProcessResponseApiSuccess()
        {
            CommonEventLogger.LogSuccessEvent(
                ref s_MrsApiProcessResponseState,
                MrsApiProcessResponseEventId + s_antiEventIdBase);
        }

        public static void LogMrsProcessResponseApiFailure(string errorMsg)
        {
            CommonEventLogger.LogFailureEvent(
                ref s_MrsApiProcessResponseState,
                MrsApiProcessResponseEventId,
                errorMsg);
        }

        public static void LogMrsCallbackCacheApiSuccess()
        {
            CommonEventLogger.LogSuccessEvent(
                ref s_MrsApiCallbackCacheState,
                MrsApiCallbackCacheEventId + s_antiEventIdBase);
        }

        public static void LogMrsCallbackCacheApiFailure(string errorMsg)
        {
            CommonEventLogger.LogFailureEvent(
                ref s_MrsApiCallbackCacheState,
                MrsApiCallbackCacheEventId,
                errorMsg);
        }

        public static void LogMrsCallbackDatabaseApiSuccess()
        {
            CommonEventLogger.LogSuccessEvent(
                ref s_MrsApiCallbackDatabaseState,
                MrsApiCallbackDatabaseEventId + s_antiEventIdBase);
        }

        public static void LogMrsCallbackDatabaseApiFailure(string errorMsg)
        {
            CommonEventLogger.LogFailureEvent(
                ref s_MrsApiCallbackDatabaseState,
                MrsApiCallbackDatabaseEventId,
                errorMsg);
        }

        public static void LogMrsCallbackResponseApiSuccess()
        {
            CommonEventLogger.LogSuccessEvent(
                ref s_MrsApiCallbackResponseState,
                MrsApiCallbackResponseEventId + s_antiEventIdBase);
        }

        public static void LogMrsCallbackResponseApiFailure(string errorMsg)
        {
            CommonEventLogger.LogFailureEvent(
                ref s_MrsApiCallbackResponseState,
                MrsApiCallbackResponseEventId,
                errorMsg);
        }

        public static void LogMrsCategorizeResponseApiSuccess()
        {
            CommonEventLogger.LogSuccessEvent(
                ref s_MrsApiCategorizeResponseState,
                MrsApiCategorizeResponseEventId + s_antiEventIdBase);
        }

        public static void LogMrsCategorizeResponseApiFailure(string errorMsg)
        {
            CommonEventLogger.LogFailureEvent(
                ref s_MrsApiCategorizeResponseState,
                MrsApiCategorizeResponseEventId,
                errorMsg);
        }

        public static void LogMrsApiGetDataBlockSuccess()
        {
            CommonEventLogger.LogSuccessEvent(
                ref s_MrsApiGetDataBlockState,
                MrsApiGetDataBlockEventId + s_antiEventIdBase);
        }

        public static void LogMrsApiGetDataBlockFailure(string errorMsg)
        {
            CommonEventLogger.LogFailureEvent(
                ref s_MrsApiGetDataBlockState,
                MrsApiGetDataBlockEventId,
                errorMsg);
        }

        public static void LogGetOverridesThreadSuccess()
        {
            CommonEventLogger.LogSuccessEvent(
                ref s_GetOverridesThreadState,
                GetOverridesThreadEventId + s_antiEventIdBase);
        }

        public static void LogGetOverridesThreadFailure(string errorMsg)
        {
            CommonEventLogger.LogFailureEvent(
                ref s_GetOverridesThreadState,
                GetOverridesThreadEventId,
                errorMsg);
        }

        public static void LogTopLevelExceptionThreadSuccess()
        {
            CommonEventLogger.LogSuccessEvent(
                ref s_TopLevelExceptionThreadState,
                TopLevelExceptionEventId + s_antiEventIdBase);
        }

        public static void LogTopLevelExceptionThreadFailure(string errorMsg)
        {
            CommonEventLogger.LogFailureEvent(
                ref s_TopLevelExceptionThreadState,
                TopLevelExceptionEventId,
                errorMsg);
        }

        public static void LogServiceStartingEvent()
        {
            CommonEventLogger.LogServiceEvent(
                "Service starting",
                ServiceStartingEventId);
        }

        public static void LogServiceStartedEvent()
        {
            CommonEventLogger.LogServiceEvent(
                "Service started",
                ServiceStartedEventId);
        }

        public static void LogServiceStoppingEvent()
        {
            CommonEventLogger.LogServiceEvent(
                "Service stopping",
                ServiceStoppingEventId);
        }

        public static void LogServiceStoppedEvent()
        {
            CommonEventLogger.LogServiceEvent(
                "Service stopped",
                ServiceStoppedEventId);
        }
    }
}
